package com.sinodata.bsm.cicp.collector.net.collector;

import java.util.ArrayList;
import java.util.List;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.union.net.snmp.SnmpGet;

public class NetEquipmentCollector extends Collector {

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        SnmpGet snmpGet;
        try {
            snmpGet = new SnmpGet(ip);
            String community = this.paramMap.get("共同体");
            snmpGet.setCommunity(community);
            String[] propMetas = getPropMeta();
            List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propValues.length; i++) {
                String vs = snmpGet.getSingle("");
                propValues[i] = ResPropertyValue.createPropValue(vs, propMetas[i]);
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception e) {
            logger.error("get informix lock rate information failed! ", e);
            throw new CollectorException(e);
        }
        return null;
    }

    public String[] getParamMeta() {
        return new String[] { "共同体" };
    }

    public String[] getPropMeta() {
        int count = Integer.parseInt(this.paramMap.get("字段数"));
        String[] propMetas = new String[count];
        for (int i = 1; i <= propMetas.length; i++) {
            propMetas[i - 1] = "参数" + toChineseString(i);
        }
        return propMetas;
    }

    /**
     * 
     * 
     * @param number
     * @return
     */
    private static String toChineseString(int number) {
        if (number == 0) {
            return "零";
        }
        String chineseString = "";
        int n = number / 10;
        if (n > 0) {
            if (n > 1) {
                chineseString += toChineseNumber(n);
            }
            chineseString += "十";
        }
        n = number % 10;
        if (n > 0) {
            chineseString += toChineseNumber(n);
        }
        return chineseString;
    }

    private static String toChineseNumber(int number) {
        String[] chineseNumbers = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        return chineseNumbers[number];
    }
}
